<?php
require_once('../entry.php');
require_once('../config/config.inc.php');
require_once 'common.php';

require_once LIB_DIR.'model/Movie.dao.php';

if(file_exists( CRAWLER_DELTA_FILE )){
    $movieDao = new MovieDAO();
    
    $strIds = file_get_contents( CRAWLER_DELTA_FILE);
    $ids = explode( "\n", $strIds );
    
    foreach( $ids as $id ){
        $id =  intval($id);
        $filePath = CRAWLER_DATA.floor( $id/100 )."/".$id.'.php';
        if(file_exists( $filePath ) ){
            $itemInfo = require $filePath;
            
            $data = $itemInfo['important'];
            $data['id'] = $id;
            $data['title'] = $itemInfo['title'];
            $data['updatetime'] = date( 'Y-m-d H:i:s', strtotime( $itemInfo['updateTime'] ));
            $data['imgUrl'] = $itemInfo['imgUrl'];
            $data['description'] = $itemInfo['intro']['简介'];
            // 电影或者电视剧
            if( $itemInfo['type'] == "纪录片" ){
                $data['movietype'] = "3";
            }
            else if( $itemInfo['type'] == "公开课" ){
                $data['movietype'] = "4";
            }
            else if( $itemInfo['type'] == "专题" ){
                $data['movietype'] = "5";
            }
            else if( $itemInfo['type'] == "电影" ){
                $data['movietype'] = "2";
            }
            else{
                // 默认为电视剧
                $data['movietype'] = "1";
            }

            if( !$movieDao->CheckMovieExist($id) ){
                // insert into DB
                $movieDao->Add( $data );
            }
            else{
                $movieDao->Update( $id, $data );
            }
        }
        else{
            echo $id." does not exist\n";
        }
    }
}
else{
    echo "no item to process.";
}
?>
